<template>
  <div class="form-single-fragment" style="position: relative;">
    <el-form ref="SpecialCheckList" :model="formData" class="full-width-input" :rules="rules" style="width: 100%;"
             label-width="100px" :size="defaultFormItemSize" label-position="right" @submit.native.prevent>
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="项目" prop="SpecialCheck.proId">
            <el-select class="input-item"
                    v-model="formData.SpecialCheck.proId"
                    :clearable="true"
                    filterable
                    placeholder="项目"
                    :loading="SpecialCheckList.projectId.impl.loading"
                    @visible-change="SpecialCheckList.projectId.impl.onVisibleChange"
                    @change="onProjectIdValueChange">
              <el-option v-for="item in SpecialCheckList.projectId.impl.dropdownList"
                         :key="item.id" :value="item.id" :label="item.name"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="合同" prop="SpecialCheck.sectionId">
            <el-select class="input-item" v-model="formData.SpecialCheck.sectionId"
                       :clearable="true"
                       filterable placeholder="合同"
                       :loading="SpecialCheckList.contractId.impl.loading"
                       @visible-change="SpecialCheckList.contractId.impl.onVisibleChange"
                       @change="onContractIdValueChange">
              <el-option v-for="item in SpecialCheckList.contractId.impl.dropdownList"
                         :key="item.id" :value="item.id" :label="item.name"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="检查描述" prop="SpecialCheck.checkDescribe">
            <el-input class="input-item" v-model="formData.SpecialCheck.checkDescribe"
                      :clearable="true" placeholder="检查描述"/>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="检查报告" prop="SpecialCheck.checkReport">
            <el-input class="input-item" v-model="formData.SpecialCheck.checkReport"
                      :clearable="true" placeholder="检查报告"/>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="整改状态" prop="SpecialCheck.changeStaus">
            <el-select class="input-item" v-model="formData.SpecialCheck.changeStaus"
                         filterable
                         :clearable="true"
                         placeholder="整改状态"
                         @visible-change="SpecialCheckList.changeStaus.impl.onVisibleChange"
                         @change="onChangeStausChange">
              <el-option v-for="item in SpecialCheckList.changeStaus.impl.dropdownList"
                         :key="item.id" :value="item.id" :label="item.name"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="12">
          <el-form-item label="检测单位" prop="SpecialCheck.checkOrgType">
            <el-select class="input-item" v-model="formData.SpecialCheck.checkOrgType"
                         filterable
                         :clearable="true"
                         placeholder="检测单位"
                         @visible-change="SpecialCheckList.checkOrgType.impl.onVisibleChange"
                         @change="onCheckOrgTypeChange">
              <el-option v-for="item in SpecialCheckList.checkOrgType.impl.dropdownList"
                         :key="item.id" :value="item.id" :label="item.name"/>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="备注" prop="SpecialCheck.remark">
            <el-input class="input-item"
                      v-model="formData.SpecialCheck.remark"
                      :clearable="true"
                      type="textarea"
                      placeholder="备注"/>
          </el-form-item>
        </el-col>
        <el-col :span="24" class="gutter-bottom">
          <el-row class="no-scroll flex-box" type="flex" justify="end">
            <el-button type="primary" v-if="!isEdit"
                       :size="defaultFormItemSize" @click="onAddClick">
               新增
            </el-button>
            <el-button type="primary" v-if="isEdit" :size="defaultFormItemSize"
                       @click="onUpdateClick()">
              保存
            </el-button>
            <el-button type="primary" :size="defaultFormItemSize" :plain="true"
                       @click="onCancel(false)">
              取消
            </el-button>
          </el-row>
        </el-col>
      </el-row>
    </el-form>
  </div>
</template>

<script>
  import { findTreeNode, findTreeNodePath, findItemFromList } from '@/utils';
  import { DropdownWidget, TableWidget, UploadWidget, ChartWidget } from '@/utils/widget.js';
  import { uploadMixin, statsDateRangeMixin, cachePageMixin } from '@/core/mixins';
  import { SpecialCheckController, DictionaryController } from '@/api';

    export default {
      name: "specialCheckForm",
      props: {
        // 是否支持级联操作
        supportCascadeAdd: {
          type: String,
          default: '0'
        },
        // 级联操作数据
        rowData: {
          type: Object
        },
        id: {
          default: undefined
        }
      },
      mixins: [uploadMixin, statsDateRangeMixin],
      data () {
        return {
          formData: {
            SpecialCheck: {
              specialCheckId: undefined,
              proId: undefined,
              sectionId: undefined,
              checkDescribe: undefined,
              checkReport: undefined,
              changeStaus: undefined,
              checkOrgType: undefined,
              remark:undefined,
              addTime:undefined,
              isDatasourceInit: false
            }
          },
          rules: {
            'SpecialCheck.proId': [
              {required: true, message: '请输入项目', trigger: 'blur'}
            ],
            'SpecialCheck.sectionId': [
              {required: true, message: '请输入合同', trigger: 'blur'}
            ],
          },
          SpecialCheckList: {
            formFilter: {},
            formFilterCopy: {},
            btnGroup:{
              isInit: false
            },
            changeStaus: {
              impl: new DropdownWidget(this.loadChangeStausDropdownList, true, 'id', 'parentId'),
              value: []
            },
            checkOrgType: {
              impl: new DropdownWidget(this.loadCheckOrgTypeDropdownList,true,'id','parentId'),
              value: []
            },
            projectId: {
              impl: new DropdownWidget(this.loadProjectIdDropdownList,true,'id','parentId'),
              value: []
            },
            contractId: {
              impl: new DropdownWidget(this.loadContractIdDropdownList)
            },
            IsInit: false
          }

        }
      },
      methods: {
        onCancel (isSuccess, data) {
          if (this.observer != null) {
            this.observer.cancel(isSuccess, data);
          }
        },
        /**
         * 更新进场人员审查表单页
         */
        refreshSpecialCheckList (reloadData = false) {
          this.loadSpecialCheckData().then(res => {
            if (!this.SpecialCheckList.isInit) {
              // 初始化下拉数据
            }
            this.SpecialCheckList.isInit = true;
          }).catch(e => {});
        },
        /**
         * 新增
         */
        onAddClick () {
          this.$refs.SpecialCheckList.validate((valid) => {
            if (!valid) return;
            if (this.supportCascadeAdd === '1') {
              this.onCancel(true);
              return;
            }

            let params = {
              specialCheckDto: {
                proId: this.formData.SpecialCheck.proId,
                sectionId: this.formData.SpecialCheck.sectionId,
                checkDescribe: this.formData.SpecialCheck.checkDescribe,
                checkReport: this.formData.SpecialCheck.checkReport,
                changeStaus: this.formData.SpecialCheck.changeStaus,
                checkOrgType: this.formData.SpecialCheck.checkOrgType
              }
            };

            SpecialCheckController.add(this,params).then(res => {
              this.$message.success('新增成功');
              this.onCancel(true);
            }).catch(e => {});
          })
        },
        /**
         * 编辑
         */
        onUpdateClick () {
          this.$refs.SpecialCheckList.validate((valid) => {
            if (!valid) return;
            // 如果是级联表格的添加或者编辑弹窗
            if (this.supportCascadeAdd === '1') {
              let formData = {
                projectIdDictMap: findItemFromList(this.SpecialCheckList.projectId.impl.dropdownList,this.formData.SpecialCheck.proId,'id'),
                changeStausDictMap: findItemFromList(this.SpecialCheckList.changeStaus.impl.dropdownList,this.formData.SpecialCheck.changeStaus,'id'),
                checkOrgTypeDictMap: findItemFromList(this.SpecialCheckList.checkOrgType.impl.dropdownList,this.formData.SpecialCheck.checkOrgType,'id'),
                contractIdDictMap: findItemFromList(this.SpecialCheckList.contractId.impl.dropdownList,this.formData.SpecialCheck.sectionId,'id'),
                ...this.formData.SpecialCheck
              };
              this.onCancel(true,formData);
              return;
            }

            let params = {
              specialCheckDto: {
                specialCheckId: this.formData.SpecialCheck.specialCheckId,
                proId: this.formData.SpecialCheck.proId,
                sectionId: this.formData.SpecialCheck.sectionId,
                checkDescribe: this.formData.SpecialCheck.checkDescribe,
                checkReport: this.formData.SpecialCheck.checkReport,
                changeStaus: this.formData.SpecialCheck.changeStaus,
                addTime: this.formData.SpecialCheck.addTime,
                checkOrgType: this.formData.SpecialCheck.checkOrgType
              }
            };

            SpecialCheckController.update(this, params).then(res => {
              this.$message.success('编辑成功');
              this.onCancel(true);
            }).catch(e => {});
          });
        },
        /**
         * 获取表详细信息
         */
        loadSpecialCheckData () {
          return new Promise(((resolve, reject) => {
            if (!this.formData.SpecialCheck.isDatasourceInit) {
              if (
                this.id == null
              ) {
                this.resetFormData();
                reject();
                return;
              }
              let params = {
                id: this.id
              };
              SpecialCheckController.view(this,params).then(res => {
                this.formData.SpecialCheck = {...res.data, isDatasourceInit:true};
                if (this.formData.SpecialCheck.projectIdDictMap && this.SpecialCheckList.projectId.impl.dirty) {
                  this.SpecialCheckList.projectId.impl.dropdownList = [this.formData.SpecialCheck.projectIdDictMap];
                }
                if (this.formData.SpecialCheck.changeStausDictMap && this.SpecialCheckList.changeStaus.impl.dirty) {
                  this.SpecialCheckList.changeStaus.impl.dropdownList = [this.formData.SpecialCheck.changeStausDictMap];
                }
                if (this.formData.SpecialCheck.checkOrgTypeDictMap && this.SpecialCheckList.checkOrgType.impl.dirty) {
                  this.SpecialCheckList.checkOrgType.impl.dropdownList = [this.formData.SpecialCheck.checkOrgTypeDictMap];
                }
                if (this.formData.SpecialCheck.contractIdDictMap && this.SpecialCheckList.contractId.impl.dirty) {
                  this.SpecialCheckList.contractId.impl.dropdownList = [this.formData.SpecialCheck.contractIdDictMap];
                }
                resolve();
              }).catch(e => {
                reject();
              });
            } else {
              this.formData.SpecialCheck.isDatasourceInit = true;
              resolve();
            }
          }))
        },
        /**
         * 重置表单数据
         */
        resetFormData () {
          this.formData = {
            SpecialCheck: {
              specialCheckId: this.formData.SpecialCheck.specialCheckId,
              proId: undefined,
              sectionId: undefined,
              checkDescribe: undefined,
              checkReport: undefined,
              changeStaus: undefined,
              checkOrgType: undefined,
              isDatasourceInit: false
            }
          }
        },
        /**
         * 下拉数据获取函数
         */
        loadChangeStausDropdownList () {
          return new Promise(((resolve, reject) => {
            let params = {};
            DictionaryController.dictChangeStaus(this,params).then(res => {
              resolve(res.getList());
            }).catch(e => {
              reject(e);
            })
          }))
        },
        /**
         * 下拉数据获取函数
         */
        loadCheckOrgTypeDropdownList () {
          return new Promise(((resolve, reject) => {
            let params = {};
            DictionaryController.dictCheckOrgType(this,params).then(res => {
              resolve(res.getList());
            }).catch(e => {
              reject(e);
            })
          }))
        },
        /**
         * 下拉数据获取函数
         */
        loadProjectIdDropdownList () {
          return new Promise((resolve, reject) => {
            let params = {};
            DictionaryController.dictSysProject(this,params).then(res => {
              resolve(res.getList());
            }).catch(e => {
              reject(e);
            });
          });
        },
        /**
         * 下拉数据获取函数
         */
        loadContractIdDropdownList () {
          return new Promise((resolve, reject) => {
            let params = {
              projectId: this.formData.SpecialCheck.proId
            };
            DictionaryController.dictSysContractInfo(this,params).then(res => {
              resolve(res.getList());
            }).catch(e => {
              reject(e);
            });
          });
        },
        /**
         * 选中值改变
         */
        onProjectIdValueChange (value) {
          this.formData.SpecialCheck.sectionId = undefined;
          this.SpecialCheckList.contractId.impl.dirty = true;
          this.onContractIdValueChange(this.formData.SpecialCheck.sectionId);
        },
        /**
         * 选中值改变
         */
        onChangeStausChange (value) {
        },
        /**
         * 选中值改变
         */
        onCheckOrgTypeChange (value) {
        },
        /**
         * 选中值改变
         */
        onContractIdValueChange (value) {
        },
        formInit () {
          this.refreshSpecialCheckList();
        }
      },
      computed: {
        isEdit () {
          return this.id != null && this.id !== '';
        }
      },
      mounted () {
        // 初始化页面数据
        this.formInit();
        // 初始化级联数据
        if (this.supportCascadeAdd === '1' && this.rowData != null) {
          this.formData.SpecialCheck = {
            ...this.rowData
          }
        }
      },
      watch: {}
    }
</script>

<style scoped>

</style>
